<?php
/**
 * Finecms函数库
 */


/**
 * url函数
 *
 * @param	string	$url		URL规则，如home/index
 * @param	array	$query		相关参数
 * @return	string	项目入口文件.php?参数
 */
function url($url, $query = array(), $self = SELF) {
    $url = str_replace('admin/', '', $url);
    return dr_url($url, $query, $self);
}
function dr_url($url, $query = array(), $self = SELF) {

    if (!$url) {
        return $self;
    }

    $url = strpos($url, 'admin') === 0 ? substr($url, 5) : $url;
    $url = trim($url, '/');

    // 判断是否后台首页
    if ($self != 'index.php' && ($url == 'home/index' || $url == 'home/home')) {
        return SELF;
    }

    $url = explode('/', $url);
    $uri = array();

    switch (count($url)) {
        case 1:
            $uri['c'] = 'home';
            $uri['m'] = $url[0];
            break;
        case 2:
            $uri['c'] = $url[0];
            $uri['m'] = $url[1];
            break;
        case 3:
            $uri['s'] = $url[0];
            $uri['c'] = $url[1];
            $uri['m'] = $url[2];
            break;
    }

    $query && $uri = @array_merge($uri, $query);

    $string = '';
    foreach ($uri as $n => $v) {
        $n == 'page' && $v = urldecode($v);
        $string.= $n.'='.$v.'&';
    }

    return SITE_URL.$self.'?'.trim($string, '&');
}

/**
 * 当前URL
 */
function dr_now_url() {

    $pageURL = 'http';
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' && $pageURL.= 's';

    $pageURL.= '://';
    if (strpos($_SERVER['HTTP_HOST'], ':') !== FALSE) {
        $url = explode(':', $_SERVER['HTTP_HOST']);
        $url[0] ? $pageURL.= $_SERVER['HTTP_HOST'] : $pageURL.= $url[0];
    } else {
        $pageURL.= $_SERVER['HTTP_HOST'];
    }

    $pageURL.= $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];

    return $pageURL;
}


/**
 * 多语言输出
 *
 * @param	多个参数
 * @return	string|NULL
 */
function dr_lang() {

    $param = func_get_args();
    if (empty($param)) {
        return NULL;
    }

    // 取第一个作为语言名称
    $string = $param[0];
    unset($param[0]);


    // 替换
    $string = get_instance()->replace_lang($string);

    $string = $param ? vsprintf($string, $param) : $string;


    return $string;
}

function lang($a) {
    return dr_lang($a);
}